package com.oscar.ehome.utils.imageview;

import android.app.Activity;
import android.app.Fragment;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;


import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;

import net.tycmc.bulb.androidstandard.utils.JsonUtils;
import net.tycmc.bulb.androidstandard.utils.ProgressUtil;

import org.apache.commons.lang.StringUtils;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ChuliPhoto {

    private static FileCache fileCache;
    private static Context context;
    private static Fragment fra;
    File file_path;
    private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
    private MemoryCache memoryCache = new MemoryCache();
    // 线程池
    private ExecutorService executorService;
    private static int img = 0;

    public ChuliPhoto(Context setingActivity) {
        // TODO Auto-generated constructor stub
        fileCache = new FileCache(context);
        ChuliPhoto.context = setingActivity;
        executorService = Executors.newFixedThreadPool(5);
    }

    public ChuliPhoto(Fragment fra) {
        // TODO Auto-generated constructor stub
        fileCache = new FileCache(context);
        ChuliPhoto.fra = fra;
        executorService = Executors.newFixedThreadPool(5);
    }

    public String changePhotoPath(String photopath) {
        String[] data = StringUtils.split(photopath, "/");
        String temp = "SmallImg/" + data[data.length - 1];
        String path = "http://";
        for (int i = 1; i < data.length - 1; i++) {
            path += data[i] + "/";
        }
        path += temp;
        return path;
    }

    /**
     * 得到地址生成图片
     *
     * @return 返回值描述
     */
    public static Bitmap getBitMap() {

        BitmapFactory.Options opt = new BitmapFactory.Options();
        // 这个isjustdecodebounds很重要
        opt.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/userhead.jpg", opt);
        int picWidth = opt.outWidth;
        int picHeight = opt.outHeight;
        Log.d("picWidth", "picWidth" + picWidth);
        Log.d("picHeight", "picHeight" + picHeight);
        int scaleX = picWidth / 1024;
        int scaleY = picHeight / 1024;
        int scale = scaleX > scaleY ? scaleX : scaleY;
        if (scale < 1) {
            opt.inSampleSize = 1;
        } else if (scale >= 1 && scale < 2) {
            opt.inSampleSize = 2;
        } else if (scale >= 2 && scale < 3) {
            opt.inSampleSize = 3;
        } else if (scale >= 3 && scale < 4) {
            opt.inSampleSize = 4;
        } else if (scale >= 4) {
            opt.inSampleSize = 5;
        }
        opt.inJustDecodeBounds = false;
        Bitmap newBitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/userhead.jpg", opt);
        return newBitmap;
    }

    /**
     * 得到传过来的图片地址
     *
     * @return 返回值描述
     */
    public static String getPhotopath() {
        String photopath = "";
        String path = "";
        Bitmap newBitmap = getBitMap();
        int degree = readPictureDegree(Environment.getExternalStorageDirectory() + "/userhead.jpg");
        newBitmap = rotaingImageView(degree, newBitmap);

        try {

            path = android.os.Environment.getExternalStorageDirectory() + "/ehomeImg/";
            File myFilePath = new File(path);
            if (!myFilePath.exists()) {
                myFilePath.mkdir();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        photopath = path + String.valueOf(System.currentTimeMillis()) + ".jpg";

        File f = new File(photopath);
        try {
            BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
            newBitmap.compress(Bitmap.CompressFormat.JPEG, 80, os);
            os.flush();
            os.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        newBitmap.recycle();

        return photopath;
    }


    /**
     * @param fileName 图片路径名
     * @return 返回值描述
     * @throws <异常类型> {@inheritDoc} 异常描述
     * @author 作者:贾暖暖 方法描述:对图片进行缩放
     */
    public static Bitmap getphoto(String fileName) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        // 这个isjustdecodebounds很重要
        opt.inJustDecodeBounds = true;
        Bitmap bm;
        bm = BitmapFactory.decodeFile(fileName, opt);

        // 获取到这个图片的原始宽度和高度
        int picWidth = opt.outWidth;
        int picHeight = opt.outHeight;
        Log.d("Chuliphoto()", "picWidth:" + picWidth + "picHeight:" + picHeight);

        // 获取屏的宽度和高度
        /*
         * WindowManager windowManager = getWindowManager(); Display display =
		 * windowManager.getDefaultDisplay();
		 */
        int screenWidth = 600;
        int screenHeight = 800;

        // isSampleSize是表示对图片的缩放程度，比如值为2图片的宽度和高度都变为以前的1/2
        opt.inSampleSize = 1;
        // 根据屏的大小和图片大小计算出缩放比例
        if (picWidth > picHeight) {
            if (picWidth > screenWidth)
                opt.inSampleSize = picWidth / screenWidth;
        } else {
            if (picHeight > screenHeight) {
                opt.inSampleSize = picHeight / screenHeight;
            }
        }

        // 这次再真正地生成一个有像素的，经过缩放了的bitmap
        opt.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeFile(fileName, opt);
        return bm;
    }

    public static Bitmap setAlpha(Bitmap sourceImg, int number) {
        int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];
        sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值
        number = number * 255 / 100;
        for (int i = 0; i < argb.length; i++) {
            argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF);// 修改最高2位的值
        }
        sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888);
        return sourceImg;
    }

    /**
     * @return 返回值描述
     * @throws <异常类型> {@inheritDoc} 异常描述
     * @author 作者:贾暖暖 方法描述:具体功能描述 缩放图片，解决加载多张图片内存溢出问题
     */
    public static byte[] decodeBitmap(String path) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;// 设置成了true,不占用内存，只获取bitmap宽高
        BitmapFactory.decodeFile(path, opts);
        opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800);
        opts.inJustDecodeBounds = false;// 这里一定要将其设置回false，因为之前我们将其设置成了true
        opts.inPurgeable = true;
        opts.inInputShareable = true;
        opts.inDither = false;
        opts.inPurgeable = true;
        opts.inTempStorage = new byte[16 * 1024];
        FileInputStream is = null;
        Bitmap bmp = null;
        ByteArrayOutputStream baos = null;
        try {
            is = new FileInputStream(path);
            bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);
            double scale = getScaling(opts.outWidth * opts.outHeight, 1024 * 600);
            Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, (int) (opts.outWidth * scale), (int) (opts.outHeight * scale),
                    true);
            bmp.recycle();
            baos = new ByteArrayOutputStream();
            bmp2.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            bmp2.recycle();
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.gc();
        }
        return baos.toByteArray();
    }

    private static double getScaling(int src, int des) {
        /**
         * 48 目标尺寸÷原尺寸 sqrt开方，得出宽高百分比 49
         */
        double scale = Math.sqrt((double) des / (double) src);
        return scale;
    }

    public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);

        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }

        return roundedSize;
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;

        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128
                : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            return lowerBound;
        }

        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

    /**
     * 从手机选图片
     *
     * @param activity
     * @param data     图片信息
     * @return 返回描述
     */
    public static String getPhotoPath(Activity activity, Intent data, int a) {
        String userSelectPath = "";
        String sdStatus = Environment.getExternalStorageState();// 得到sd卡的状态
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {// 检测sd是否可用
            Toast.makeText(activity, "SD card 不可用！", Toast.LENGTH_SHORT).show();
            return null;
        }
        if (data == null) {
            Toast.makeText(activity, "没有选择照片！", Toast.LENGTH_SHORT).show();
            return null;
        }
        Uri uri = data.getData();
        if (uri == null) {
            return null;
        } else {
            // 获取路径---------------------------
            String[] proj = {MediaColumns.DATA};
            // 好像是android多媒体数据库的封装接口，具体的看Android文档
            Cursor cursor = activity.getContentResolver().query(uri, proj, null, null, null);
            if (cursor != null) {//小米手机会是空
                // 按我个人理解 这个是获得用户选择的图片的索引值
                int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
                // 将光标移至开头 ，这个很重要，不小心很容易引起越界
                cursor.moveToFirst();
                // 最后根据索引值获取图片路径
                userSelectPath = cursor.getString(column_index);
                if (StringUtils.isNotEmpty(userSelectPath)) {
                    ContentResolver resolver = activity.getContentResolver();
                    try {
                        Bitmap photo = MediaStore.Images.Media.getBitmap(resolver, uri);
                        if (photo != null) {
                            Bitmap bit = getphoto(userSelectPath);
                            if (a == 1) {
                                userSelectPath = saveBitmap(bit);
                            } else {
                                userSelectPath = saveBitmaps(activity, bit);
                            }
                            photo.recycle();
                        }
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    if (StringUtils.isBlank(userSelectPath)) {
                        ContentResolver resolver = activity.getContentResolver();
                        try {
                            Bitmap photo = MediaStore.Images.Media.getBitmap(resolver, uri);
                            if (photo != null) {
                                int SCALE = 800;
                            /*
                             * // 为防止原始图片过大导致内存溢出，这里先缩小原图显示，然后释放原始Bitmap占用的内存
							 * Bitmap smallBitmap = zoomBitmap(photo, SCALE,
							 * SCALE); // 释放原始图片占用的内存，防止out of memory异常发生
							 */
                                userSelectPath = saveBitmap(photo);
                                Bitmap bit = getphoto(userSelectPath);
                                if (a == 1) {
                                    userSelectPath = saveBitmap(bit);
                                } else {
                                    userSelectPath = saveBitmaps(activity, bit);
                                }
                                photo.recycle();
                            }
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            } else {
                userSelectPath = uri.getPath();
            }
        }
        return userSelectPath;
    }

    /**
     * 保存图片
     *
     * @param smallBitmap
     * @return
     */
    private static String saveBitmap(Bitmap smallBitmap) {
        String userSelectPath = "";
        String path = "";
        try {
            path = android.os.Environment.getExternalStorageDirectory() + "/ehomeImg/";
            File myFilePath = new File(path);
            if (!myFilePath.exists()) {
                myFilePath.mkdir();
            }
//			+ String.valueOf(System.currentTimeMillis())
            userSelectPath = path + "ehomeimg.jpg";
            File f = new File(userSelectPath);

            BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
            smallBitmap.compress(Bitmap.CompressFormat.JPEG, 80, os);
            os.flush();
            os.close();
            smallBitmap.recycle();
//            if (CommonUtils.isIdcard == 1) {
//                CommonUtils.idcard_file = f;
//            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return userSelectPath;
    }

    /**
     * 保存图片
     *
     * @param smallBitmap
     * @return
     */
    public static String saveBitmaps(Context contexts, Bitmap smallBitmap) {
        String userSelectPath = "";
        String path = "";
        try {
            path = android.os.Environment.getExternalStorageDirectory() + "/ehomeImg/";
            File myFilePath = new File(path);
            if (!myFilePath.exists()) {
                myFilePath.mkdir();
            }
            userSelectPath = path + "ehomeimg.jpg";
            File f = new File(userSelectPath);
            BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
            smallBitmap.compress(Bitmap.CompressFormat.JPEG, 80, os);
            os.flush();
            os.close();
            smallBitmap.recycle();
//            if (CommonUtils.isIdcard == 1) {
//                CommonUtils.idcard_file = f;
//            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return userSelectPath;
    }

    /**
     * 保存图片
     *
     * @param smallBitmap
     * @return
     */
    public static String saveBitmaps(Context contexts, Bitmap smallBitmap, String name) {
        String userSelectPath = "";
        String path = "";
        try {
            path = android.os.Environment.getExternalStorageDirectory() + "/ehomeImgs/";
            File myFilePath = new File(path);
            if (!myFilePath.exists()) {
                myFilePath.mkdir();
            }
            userSelectPath = path +"heads.jpg";
            File f = new File(userSelectPath);
            BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
            smallBitmap.compress(Bitmap.CompressFormat.JPEG, 80, os);
            os.flush();
            os.close();
            smallBitmap.recycle();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return userSelectPath;
    }

    /**
     * 缩放Bitmap图片
     **/

    public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {

        int w = bitmap.getWidth();

        int h = bitmap.getHeight();

        Matrix matrix = new Matrix();

        float scaleWidth = w > width ? ((float) width / w) : 1;

        float scaleHeight = h > height ? ((float) height / h) : 1;

        matrix.postScale(scaleWidth, scaleHeight);// 利用矩阵进行缩放不会造成内存溢出

        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);

        return newbmp;

    }

    /**
     * @param angle ：需要旋转的角度 bitmap：需要做旋转的图片
     * @return 处理完成后的图片
     * @throws <异常类型> {@inheritDoc}
     * @author 作者:whl
     * @方法描述:旋转图片
     */
    public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
        // 旋转图片 动作
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        System.out.println("angle2=" + angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }

    /**
     * 读取图片属性：旋转的角度
     *
     * @param path 图片绝对路径
     * @return degree旋转的角度
     */
    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    public static Bitmap getphotos(String fileName) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        // 这个isjustdecodebounds很重要
        opt.inJustDecodeBounds = true;
        Bitmap bm;
        bm = BitmapFactory.decodeFile(fileName, opt);

        // 获取到这个图片的原始宽度和高度
        int picWidth = opt.outWidth;
        int picHeight = opt.outHeight;
        Log.d("Chuliphoto()", "picWidth:" + picWidth + "picHeight:" + picHeight);

        int screenWidth = 600;
        int screenHeight = 800;

        // isSampleSize是表示对图片的缩放程度，比如值为2图片的宽度和高度都变为以前的1/2
        opt.inSampleSize = 1;
        // 根据屏的大小和图片大小计算出缩放比例
        if (picWidth > picHeight) {
            if (picWidth > screenWidth)
                opt.inSampleSize = picWidth / screenWidth;
        } else {
            if (picHeight > screenHeight) {
                opt.inSampleSize = picHeight / screenHeight;
            }
        }

        // 这次再真正地生成一个有像素的，经过缩放了的bitmap
        opt.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeFile(fileName, opt);
        return bm;
    }

    // 最主要的方法
    public void DisplayImage(String url, ImageView imageView, boolean isLoadOnlyFromCache, int head_img) {
        imageViews.put(imageView, url);
        // 先从内存缓存中查找
        img = head_img;
        Bitmap bitmap = memoryCache.get(url);
        if (bitmap != null)
            imageView.setImageBitmap(bitmap);
        else if (!isLoadOnlyFromCache) {

            if (StringUtils.isEmpty(url)) {
            } else {

                // 若没有的话则开启新线程加载图片
                queuePhoto(url, imageView);
            }
        }
    }

    private class PhotoToLoad {
        public String url;
        public ImageView imageView;

        public PhotoToLoad(String u, ImageView i) {
            url = u;
            imageView = i;
        }
    }

    private void queuePhoto(String url, ImageView imageView) {
        PhotoToLoad p = new PhotoToLoad(url, imageView);
        executorService.submit(new PhotosLoader(p));
    }

    private static Bitmap getBitmap(String url) {
        File f;
        try {
            f = new File(url);

            // 先从文件缓存中查找是否有
            Bitmap b = null;
            if (f != null && f.exists()) {
                b = decodeFile(f);
            }
            if (b != null) {
                return b;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 最后从指定的url中下载图片
        try {
            Bitmap bitmap = null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is = conn.getInputStream();
            String imgPath = "";
            imgPath = fileCache.getSavePath(url);
            OutputStream os = new FileOutputStream(imgPath);
            CopyStream(is, os);
            os.close();
            f = new File(imgPath);
            bitmap = decodeFile(f);

            return bitmap;
        } catch (Exception ex) {
            Log.e("ImageLoader", "getBitmap catch Exception...\nmessage = " + ex.getMessage());
            return null;
        }
    }

    private static Bitmap decodeFile(File f) {
        try {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            final int REQUIRED_SIZE = 100;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
        }
        return null;
    }

    public static void CopyStream(InputStream is, OutputStream os) {
        final int buffer_size = 1024;
        try {
            byte[] bytes = new byte[buffer_size];
            for (; ; ) {
                int count = is.read(bytes, 0, buffer_size);
                if (count == -1)
                    break;
                os.write(bytes, 0, count);
            }
        } catch (Exception ex) {
            Log.e("ImageLoader", "CopyStream catch Exception...");
        }
    }

    /**
     * 防止图片错位
     *
     * @param photoToLoad
     * @return
     */
    boolean imageViewReused(PhotoToLoad photoToLoad) {
        String tag = imageViews.get(photoToLoad.imageView);
        if (tag == null || !tag.equals(photoToLoad.url))
            return true;
        return false;
    }

    class PhotosLoader implements Runnable {
        PhotoToLoad photoToLoad;

        PhotosLoader(PhotoToLoad photoToLoad) {
            this.photoToLoad = photoToLoad;
        }

        @Override
        public void run() {
            if (imageViewReused(photoToLoad))
                return;
            Bitmap bmp = getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url, bmp);
            if (imageViewReused(photoToLoad))
                return;
            BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
            // 更新的操作放在UI线程中
            Activity a = (Activity) photoToLoad.imageView.getContext();
            a.runOnUiThread(bd);
            ProgressUtil.hide();
        }
    }

    // 用于在UI线程中更新界面
    class BitmapDisplayer implements Runnable {
        Bitmap bitmap;
        PhotoToLoad photoToLoad;

        public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
            bitmap = b;
            photoToLoad = p;
        }

        @Override
        public void run() {
            if (imageViewReused(photoToLoad))
                return;
            if (bitmap != null) {
                photoToLoad.imageView.setImageBitmap(bitmap);
            } else {
            }

        }
    }


}
